/*
 * This is the Socket Server
 */
package SocketDemo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

// Inherits from Thread as a standalone thread
public class Server extends Thread {
    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    private ServerSocket serverSocket;

    public Server(int port) {
        try {
            this.serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Server() {
        try {
            this.serverSocket = new ServerSocket(8080);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            System.out.println("Server:     Server Listening on " + serverSocket.getLocalPort() + " ...");
            while (true) {
                Socket socket = serverSocket.accept();
                // After accept the Socket, processing the socket event at the independent thread.
                SocketThread socketThread = new SocketThread(socket);
                socketThread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
